const {execSql} = require("../utils/db")
class goods{
    static async getIndexJD(){
        //查询广告位置表 看下当前查询的类型 并且 该广告位下的广告需要是展示的状态
        let list = await execSql(`select * from lv_goods where flag=1 and type=1 order by orders desc limit 5`)
        return list
    }


    static async getJDList(page=1){
        let start = (page-1)*10
        //查询广告位置表 看下当前查询的类型 并且 该广告位下的广告需要是展示的状态
        let list = await execSql(`select * from lv_goods where flag=1 and type=1 order by orders desc limit ${start},10`)
        let listCount = await execSql(`select count(*) as listLen from lv_goods where flag=1 and type=1`)
        return {list:list,listLen:listCount[0].listLen}
    }


    static async getIndexSouvenir(){
        //查询广告位置表 看下当前查询的类型 并且 该广告位下的广告需要是展示的状态
        let list = await execSql(`select * from lv_goods where flag=1 and type=2 order by orders desc limit 5`)
        return list
    }


    static async getSouvenirList(page=1){
        let start = (page-1)*10
        //查询广告位置表 看下当前查询的类型 并且 该广告位下的广告需要是展示的状态
        let list = await execSql(`select * from lv_goods where flag=1 and type=2 order by orders desc limit ${start},10`)
        let listCount = await execSql(`select count(*) as listLen from lv_goods where flag=1 and type=2`)
        return {list:list,listLen:listCount[0].listLen}
    }


    static async getGoodsDetail(id){
        let res = null
        //1.查询商品表  当时在做酒店详情的时候 做了一个spu 和sku的商品详情接口查询 =spu原理是无限极分类 sku 包含了spuid 处理成多维数组 
        let goods =await execSql(`select * from lv_goods where id=${id}`)//[{id:1....}]
        res = goods[0]
        //1.查询spu // 查询顶级spu
        // A = SELECT spuid FROM lv_goods_sku where goodsid=${id} GROUP BY spuid => 查询sku表 根据商品id  对结果的spuid进行 去重展示1,2,3
        //B = SELECT pid from lv_goods_spu WHERE id in(1,2,3) GROUP BY pid => 查询 spu表中 所对应的数据的pid的值 并且去重复
        //`SELECT * from lv_goods_spu WHERE id in(1,2,3,4)=> 得到 所有的spu 1级分类的数据 （pid不是0 代表他就一定是2级） 
        let spuSonList =await execSql(`SELECT * from lv_goods_spu WHERE id in(SELECT pid from lv_goods_spu WHERE id in(SELECT spuid FROM lv_goods_sku where goodsid=${id} GROUP BY spuid) GROUP BY pid)`)
        /**
         *  无限极分类 =》
         *  [   {id:1,name:"天津",pid:0},
         *      {id:2,name:"北辰区",pid:1},
         *      {id:3,name:"武清区",pid:1},
         *      {id:4,name:"八维",pid:2},
         *      {id:5,name:"魏纪琛",pid:4}
         *  ]
         */
        let spuSon
        for(var i=0;i<spuSonList.length;i++){
            //查询 sku所有spuid的去重复的数据 
            spuSon = await execSql(`SELECT * from lv_goods_spu WHERE id in(SELECT spuid FROM lv_goods_sku where goodsid=${id} GROUP BY spuid) and pid=${spuSonList[i].id}`)
            // 循环 每条spu的数据对应的 sku的数据有哪些
            for(var j=0;j<spuSon.length;j++){
               spuSon[j]['skuList']  = await execSql(`select * from lv_goods_sku where spuid=${spuSon[j].id} and goodsid=${id}`)
            }
            spuSonList[i]['sonList'] = spuSon;
        }
        //3查询商品相册
        res.photoList = await execSql(`select * from lv_goods_photo where goodsid=${id}`)

        res.spuList = spuSonList

        return res;
    }

    /**
     * 用户点击了 喜爱 需要让 商品表的like字段 ++
     * 首先查询一下 喜爱关联表 看这个用户是否已经添加了 如果已经添加了 那么说明用户是 进行一个取消喜爱的操作 进而需要将 已经添加的数据进行删除 同时在商品表中 也需要将喜爱数量进行 减减
     * 如果用户没有添加 就需要 将这个数据添加到喜爱表中 同时 让商品表 喜爱数量 加加
     */
    static async checkLike(uid,goodsid){
        //查询用户是否已经添加这个“喜爱”  如果已经添加了 那就得删除了
        let likes = await execSql(`select * from lv_user_like where uid=${uid} and goodsid=${goodsid}`)
        if(likes.length>0){
            await execSql(`delete from lv_user_like where uid=${uid} and goodsid=${goodsid}`)
            await execSql(`update lv_goods set likenum=likenum-1 where id=${goodsid}`)
        }else{
            await execSql(`insert into lv_user_like values(null,${uid},${goodsid})`)
            await execSql(`update lv_goods set likenum=likenum+1 where id=${goodsid}`)
        }
        return true

        //点击了爱心 =》 用户没有登录 =》vuex =》本地存储 =》登录了=》备忘录提示  =》亮点业务 [{type:"like",goodsid:1},{type:"like",goodsid:2}]
        //更新商品表喜欢的数量
        // let res = await execSql(`update lv_goods set like=like+1 where id=${id}`)
        //将这个喜欢 和用户进行关联
    }

    /**
     * 获取我喜爱的数据
     * @param {Number} uid 用户id
     * @param {Number} page 当前页码 
     * @param {Number} limit 每页显示条数
     * @returns 
     */
    static async getUserLike(uid,page=1,limit=10){
        let start = (page-1)*limit
        //查询我喜欢的列表 且进行分页展示
        let list = await execSql(`select * from lv_goods where id in(select goodsid from lv_user_like where uid=${uid}) limit ${start},${limit}`)
        //查询我喜欢的所有商品的数量
        let pageCount = await execSql(`select count(*) as pageCount from lv_user_like where uid=${uid}`)
        list.pageCount = pageCount[0]['pageCount']
        return list

    }
    /**
     * 查询用户喜爱状态
     * @param {*} uid 
     * @returns 
     */
    static async likeFlag(uid){
        //  let users = ctx.jwt.check(ctx.header.authorization)
        let likes = await execSql(`select * from lv_user_like where uid=${uid} and goodsid=${goodsid}`)
        if(likes.length>0){
            return 1
        }else{
            return 2
        }
    }

    static async createNewGoods(page=1,limit=50){
        let start =(page-1)*limit
        let res = await execSql(`select * from tp_goods limit ${start},${limit}`)
        // console.log(res)
        let newArr =""
        for(var i=0;i<res.length;i++){
            newArr+=`INSERT INTO lv_goods (id,goodsname, kerword, gdesc, content, collect, likenum, looknum, shopping, flag, orders, is_new, is_hot, type, img,price) VALUES ('${res[i].goods_id}','${res[i].goods_name}', '${res[i].keywords}', '${res[i].goods_remark}', '${res[i].goods_content}', '${res[i].virtual_collect_sum}', '${res[i].collect_sum}', '${res[i].virtual_comment_count}', '${res[i].virtual_sales_sum}', '1', '0', '${res[i].is_new}', '${res[i].is_hot}', '${i%2+1}', '${res[i].original_img.replace("/public/upload", "")}',${res[i].shop_price});`
        }
        return newArr
    }

    // INSERT INTO `lv_goods_photo` (`goodsid`, `type`, `path`, `thubpath`, `skuid`) VALUES ('1', '1', '1', '1', '1')


     static async createNewGoodsPhoto(page=1,limit=50){
        let start =(page-1)*limit
        let res = await execSql(`select * from tp_goods_images limit ${start},${limit}`)
        // console.log(res)
        let newArr =""
        for(var i=0;i<res.length;i++){
            newArr+=`INSERT INTO lv_goods_photo (goodsid, type, path) VALUES ('${res[i].goods_id}', '1', '${res[i].image_url.replace("/public/upload", "")}');`
        }
        return newArr
    }

}
module.exports=goods